package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.ArticlePageDto;
import com.itheima.dto.PageDto;
import com.itheima.entity.YddArticle;
import com.itheima.mapper.YddArticleMapper;
import com.itheima.service.YddArticleService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class YddArticleServiceImpl extends ServiceImpl<YddArticleMapper,YddArticle> implements YddArticleService {
    /**
     * 前台热门置顶文章数据(首页
     * @param page
     * @param limit
     * @return
     */
    @Override
    public ArticlePageDto indexnew(String page, String limit) {

        Page<YddArticle> yddArticlePage = new Page<>();
        LambdaQueryWrapper<YddArticle> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(YddArticle::getIstop,1);
        Page<YddArticle> page1 = this.page(yddArticlePage, queryWrapper);

        int count = this.count(queryWrapper);

        List<YddArticle> records = page1.getRecords();
        long total = page1.getTotal();

        //封装pageDto
        PageDto pageDto = new PageDto();
        pageDto.setData(records);

        pageDto.setTotalPages(total);

        int pageInt = Integer.parseInt(page);
        pageDto.setCurrentPage((long) pageInt);

        int limitInt = Integer.parseInt(limit);
        pageDto.setPageSize((long) limitInt);

        pageDto.setCount((long) count);


        ArticlePageDto articlePageDto = new ArticlePageDto();
        articlePageDto.setDesc("首页大列表");
        articlePageDto.setList(pageDto);
        return articlePageDto;
    }
}
